package com.snailxr.base.task.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import com.mongodb.WriteResult;
import com.snailxr.base.task.dao.ScheduleJobDao;
import com.snailxr.base.task.domain.ScheduleJob;
@Repository("scheduleJobDaoImpl")
public class ScheduleJobDaoImpl implements ScheduleJobDao {
	public final Logger log = Logger.getLogger(this.getClass());
	@Resource(name="mongoTemplate")
	private MongoTemplate mongoTemplate;
	private String collectionName;//集合名
	
	@Override
	public boolean deleteByPrimaryKey(Long jobId) {
		Query query = new Query(Criteria.where("jobId").is(jobId));
		mongoTemplate.remove(query, ScheduleJob.class);
		return true;
	}

	@Override
	public int insert(ScheduleJob record) {
		// TODO 自动生成的方法存根
		return 0;
	}

	@Override
	public int insertSelective(ScheduleJob record) {
		mongoTemplate.insert(record, collectionName);
		return 1;
	}

	@Override
	public ScheduleJob selectByPrimaryKey(Long jobId) {
		ScheduleJob job = mongoTemplate.findOne(new Query(Criteria.where("jobId").is(jobId)), ScheduleJob.class);
		return job;
	}

	@Override
	public boolean updateByPrimaryKeySelective(ScheduleJob record) {
		log.info(record.toString());
		Query query = new Query(Criteria.where("jobId").is(record.getJobId()));
		Update update =  new Update();
		//设置字段值，少设置一个都有可能更新不成功
		update.set("jobStatus", record.getJobStatus());
		update.set("createTime", record.getCreateTime());
		update.set("jobName", record.getJobName());
		update.set("jobGroup", record.getJobGroup());
		update.set("jobStatus",record.getJobStatus());
		update.set("cronExpression",record.getCronExpression());
		update.set("description",record.getDescription());
		update.set("beanClass", record.getBeanClass());
		update.set("isConcurrent",record.getIsConcurrent());
		update.set("springId",record.getSpringId());
		update.set("methodName",record.getMethodName());
		WriteResult writeResult =  mongoTemplate.updateFirst(query,update, ScheduleJob.class);
		log.info(writeResult.toString());
		return true;
	}

	@Override
	public int updateByPrimaryKey(ScheduleJob record) {
		// TODO 自动生成的方法存根
		return 0;
	}

	@Override
	public List<ScheduleJob> getAll() {
		List<ScheduleJob> jobs =  mongoTemplate.findAll(com.snailxr.base.task.domain.ScheduleJob.class, "scheduleJob");
		return jobs;
	}

	public String getCollectionName() {
		return collectionName;
	}

	public void setCollectionName(String collectionName) {
		this.collectionName = collectionName;
	}

}
